package com.mdh.runerrands.controller;

import com.alibaba.fastjson.JSON;
import com.mdh.runerrands.Util.Result;
import com.mdh.runerrands.pojo.AccountEmployee;
import com.mdh.runerrands.pojo.BaseUser;
import com.mdh.runerrands.pojo.QueryPage;
import com.mdh.runerrands.service.AccountEmpService;
import com.mdh.runerrands.service.BaseUserService;
import com.sun.org.apache.xpath.internal.operations.Or;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/controller/waiter")
public class WaiterController {
    @Resource
    AccountEmpService accountEmpService;
    @Resource
    BaseUserService baseUserService;
    @ApiOperation(value = "根据员工id查询员工收益信息")
    @RequestMapping(value = "/detailsShou",method = RequestMethod.GET)
    public Result detailsShou(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            List list = new ArrayList();
            List<AccountEmployee> employeeList = accountEmpService.findById(id);
            for (AccountEmployee AccountEmployee :employeeList){
                if ("收益".equals(AccountEmployee.getType())){
                    list.add(AccountEmployee);
                }
            }
            result.setData(list);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setStatus("500");
            result.setMessage("Exception");
            return  result;
        }
    }
    @ApiOperation(value = "通过id删除服务员信息")
    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    public Result deleteById(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            int i = baseUserService.deleteBaseUserById(id);
            if (i > 0){
                result.setMessage("success");
                result.setData(i);
                return result;
            }
            result.setData(null);
            result.setMessage("删除服务员失败");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setStatus("500");
            result.setMessage("Exception");
            return result;
        }
    }
    @ApiOperation("通过id查询服务员信息")
    @RequestMapping(value = "/findWaiterById",method = RequestMethod.GET)
    public Result findWaiterById(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            BaseUser waiter = baseUserService.findBaseUserById(id);
            if (waiter!=null &&"waiter".equals(waiter.getType())){
                result.setMessage("success");
                result.setData(waiter);
                return result;
            }
            result.setData(null);
            result.setMessage("查询该服务员失败");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            return result;
        }
    }
    @ApiOperation(value = "查询所有服务员信息")
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll(){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            List<BaseUser> baseUserList = baseUserService.findBaseUserAll();
            if (baseUserList.isEmpty()){
                result.setData(null);
                result.setMessage("success");
                return result;
            }
            List<BaseUser> list = new ArrayList();
            for (BaseUser user : baseUserList){
                if ("waiter".equals(user.getType())){
                    list.add(user);
                }
            }
            result.setData(list);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setStatus("500");
            result.setMessage("Exception");
            return result;
        }
    }
    @ApiOperation(value = "员工注册")
    @RequestMapping(value = "/waiterRegister",method = RequestMethod.POST)
    public Result waiterRegister(BaseUser baseUser){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            int insert = baseUserService.insert(baseUser);
            if (insert>0){
                result.setData(insert);
                result.setMessage("success");
            }else {
                result.setData(null);
                result.setMessage("注册失败");
            }
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("200");
            return result;
        }
    }
    @ApiOperation(value = "保存或者更新员工信息")
    @RequestMapping(value ="/saveOrUpdate",method = RequestMethod.POST)
    public Result saveOrUpdate(BaseUser baseUser){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            if (baseUser.getId()==0){
                int insert = baseUserService.insert(baseUser);
                if(insert>0){
                    result.setMessage("success");
                    result.setData(insert);
                }else {
                    result.setData(null);
                    result.setMessage("保存员工信息失败");
                }
                return result;
            }else {
                int update = baseUserService.update(baseUser);
                if (update > 0){
                    result.setData(update);
                    result.setMessage("success");
                }else {
                    result.setData(null);
                    result.setMessage("更新员工信息失败");
                }
                return result;
            }
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("批量删除服务员")
    @RequestMapping(value = "/batchDelete",method = RequestMethod.POST)
    public Result batchDelete(int ...ids){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            boolean b = baseUserService.batchDelete(ids);
            result.setData(b);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("分页查询服务员信息")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Result query(int page,int pageSize,String telephone,String realname,String idcard,String status){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            QueryPage queryPage = new QueryPage();
            queryPage.setPage(page);
            queryPage.setPageSize(pageSize);
            List<BaseUser> query = baseUserService.query(page, pageSize, telephone, realname, idcard, status);

            queryPage.setList(query);
            queryPage.setTotal(baseUserService.waiterCount(telephone,realname,idcard,status));
            result.setData(queryPage);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }

    @ApiOperation("分页查询员工提现和收益信息")
    @RequestMapping(value = "/queryShou",method = RequestMethod.POST)
    public Result queryShou(int page,int pageSize,String money,String type,String description){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            QueryPage queryPage = new QueryPage();
            List<AccountEmployee> query = accountEmpService.query(page, pageSize, money, type, description);
            queryPage.setList(query);
            queryPage.setPage(page);
            queryPage.setPageSize(pageSize);
            queryPage.setTotal(accountEmpService.selectCount(money,type,description));
            result.setData(queryPage);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("拒绝员工身份认证审核")
    @RequestMapping(value = "/refuseAuditing",method = RequestMethod.POST)
    public Result refuseAuditing(int id){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            BaseUser baseUser = new BaseUser();
            baseUser.setId(id);
            baseUser.setStatus("禁用");
            int update = baseUserService.update(baseUser);
            result.setData(update);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("员工提现申请")
    @RequestMapping(value = "/waiterCrash",method = RequestMethod.GET)
    public Result waiterCrash(int orderId,int waiterId){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            boolean b = accountEmpService.updateWaiterCrash(orderId, waiterId);
            result.setMessage("success");
            result.setData(b);
            return result;
        }catch (Exception e){
            result.setData(JSON.toJSONString(e));
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("员工提现审核通过")
    @RequestMapping(value = "/waiterExamine",method = RequestMethod.POST)
    public Result waiterExamine(int orderId,int waiterId){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            boolean b = accountEmpService.updateWaiterExamine(orderId, waiterId);
            result.setMessage("success");
            result.setData(b);
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("员工提现审核拒绝")
    @RequestMapping(value = "/waiterRefuseExamine",method = RequestMethod.POST)
    public Result waiterRefuseExamine(int orderId,int waiterId){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            boolean b = accountEmpService.updateWaiterRefuseExamine(orderId, waiterId);
            result.setMessage("success");
            result.setData(b);
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }

    }
    @ApiOperation("员工认证审核通过")
    @RequestMapping(value = "/Result",method = RequestMethod.GET)
    public Result auditing(int waiterId){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            BaseUser baseUser = new BaseUser();
            baseUser.setId(waiterId);
            baseUser.setStatus("启用");
            int update = baseUserService.update(baseUser);
            result.setData(update);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("根据员工id查找体现信息")
    @RequestMapping(value = "/income",method = RequestMethod.GET)
    public Result incomeById(Integer id){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            List list = new ArrayList();
            List<AccountEmployee> employeeList = accountEmpService.findById(id);
            for (AccountEmployee AccountEmployee :employeeList){
                if ("提现".equals(AccountEmployee.getType())){
                    list.add(AccountEmployee);
                }
            }
            result.setData(list);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
}

